<template>
  <div class="automatic_grant">
    <div class="content-box">
      <div class="ticket-box">
        <p class="title">
          车主注册后发放停车券
          <el-button size="mini" type="success" @click="recordShow = true,type = 1">发放记录</el-button>
        </p>
        <el-select v-model="valueOne"  @change="changeRuleOne" size="mini">
          <el-option
          v-for="item in ticketRules"
          :label="item.parkingDuration+'小时'+item.sellPrice+'元'+'有效期'+item.validityDays+'天'"
          :value="item"></el-option>
        </el-select>
        <div>
          <div class="ticket-name" v-for="(item,index) in checkedRules">
            {{item.parkingDuration}}小时{{item.sellPrice}}元,有效期{{item.validityDays}}天
            <p>
              发放数量：
              <el-input-number size="mini" v-model="item.count" @change="handleChangeOne(item.count,index)" :min="1" :max="1000" label="描述文字"></el-input-number>
              张
            </p>
            <span class="close-icon" @click="deleteTicketOne(index)">X</span>
          </div>
        </div>
        <el-button class="sub-btn" size="mini" type="primary" @click="submint(1)">确认</el-button>
      </div>


      <div class="ticket-box">
        <p class="title">
          商户注册后发放停车券
          <el-button size="mini" type="success" @click="recordShow = true,type = 2">发放记录</el-button>
        </p>
        <el-select v-model="valueTwo"  @change="changeRuleTwo" size="mini">
          <el-option
          v-for="item in ticketRules"
          :label="item.parkingDuration+'小时'+item.sellPrice+'元'+'有效期'+item.validityDays+'天'"
          :value="item"></el-option>
        </el-select>
        <div>
          <div class="ticket-name" v-for="(item,index) in checkedRulesTwo">
            {{item.parkingDuration}}小时{{item.sellPrice}}元,有效期{{item.validityDays}}天
            <p>
              发放数量：
              <el-input-number size="mini" v-model="item.count" @change="handleChangeTwo(item.count,index)" :min="1" :max="1000" label="描述文字"></el-input-number>
              张
            </p>
            <span class="close-icon" @click="deleteTicketTwo(index)">X</span>
          </div>
        </div>
        <el-button class="sub-btn" size="mini" type="primary" @click="submint(2)">确认</el-button>
      </div>


      <div class="ticket-box">
        <p class="title">
          商户注册后每天发放停车券
          <el-button size="mini" type="success" @click="recordShow = true,type = 3">发放记录</el-button>
        </p>
        <el-select v-model="valueThree"  @change="changeRuleThree" size="mini">
          <el-option
          v-for="item in ticketRules"
          :label="item.parkingDuration+'小时'+item.sellPrice+'元'+'有效期'+item.validityDays+'天'"
          :value="item"></el-option>
        </el-select>
        <div>
          <div class="ticket-name" v-for="(item,index) in checkedRulesThree">
            {{item.parkingDuration}}小时{{item.sellPrice}}元,有效期{{item.validityDays}}天
            <p>
              发放数量：
              <el-input-number size="mini" v-model="item.count" @change="handleChangeThree(item.count,index)" :min="1" :max="1000" label="描述文字"></el-input-number>
              张
            </p>
            <span class="close-icon" @click="deleteTicketThree(index)">X</span>
          </div>
        </div>
        <el-button class="sub-btn" size="mini" type="primary" @click="submint(3)">确认</el-button>
      </div>
    </div>

    <el-dialog title="停车券发放记录" :visible.sync="recordShow" width="1000px" :close-on-click-modal="false" :close-on-press-escape="false">
      <record v-if="recordShow" :type="type" @closeDialog="recordShow = false" @refresh="" />
    </el-dialog>
  </div>
</template>

<script>
  import record from './automaticGrantRecord.vue'

  export default {
    components: { record },
    data() {
      return {
        valueOne: null,
        valueTwo: null,
        valueThree: null,
        ticketRules: [],
        platFormTicketRules: [],
        checkedRules: [],
        checkedRulesTwo: [],
        checkedRulesThree: [],
        recordShow: false
      }
    },

    mounted() {
      this.getTicketRule();
      this.getPlatFormTicketRule();
      this.getTicketByType(2);
    },

    methods: {
      handleChangeOne(value,index) {
        this.checkedRules[index].count = value;
      },

      handleChangeTwo(value,index) {
        this.checkedRulesTwo[index].count = value;
      },

      handleChangeThree(value,index) {
        this.checkedRulesThree[index].count = value;
      },

      getTicketByType(type) {
        this.$axios({
          method: 'get',
          url: '/business/biz-send-park-ticket/query'
        }).then((res) => {
          if(res.code == 200) {
            this.checkedRules = res.data.carOwnerRegisterList == null ? [] : res.data.carOwnerRegisterList;
            this.checkedRulesTwo = res.data.shopRegisterList == null ? [] : res.data.shopRegisterList;
            this.checkedRulesThree = res.data.shopRegisterAfterList == null ? [] : res.data.shopRegisterAfterList;
          }
        })
      },

      getTicketRule() {
        this.$axios({
          method: 'get',
          url: '/business/park-ticket-rule/page',
          params: {currentPage: 1,pageSize: 10}
        }).then((res) => {
          if(res.code == 200) {
            res.data.records.forEach(item => {
              item.count = 1;
            })
            this.ticketRules = res.data.records;
          }
        })
      },

      getPlatFormTicketRule() {
        this.$axios({
          method: 'get',
          url: '/platform/platform-ticket/page',
          params: {currentPage: 1,pageSize: 1000}
        }).then((res) => {
          if(res.code == 200) {
            res.data.records.forEach(item => {
              item.count = 1;
            })
            this.platFormTicketRules = res.data.records;
          }
        })
      },

      changeRuleOne(e) {
        let show = true;
        this.valueOne = e.parkingDuration+'小时'+e.sellPrice+'元'+'有效期'+e.validityDays+'天';
        this.checkedRules.filter(item => {
          if(item.parkTicketId == e.id || item.id == e.id) {
            show = false
          }
        })
        if(show) {
          this.checkedRules.push(e);
        }
      },

      changeRuleTwo(e) {
        let show = true;
        this.valueTwo = e.parkingDuration+'小时'+e.sellPrice+'元'+'有效期'+e.validityDays+'天';
        this.checkedRulesTwo.filter(item => {
          if(item.parkTicketId == e.id || item.id == e.id) {
            show = false
          }
        })
        if(show) {
          this.checkedRulesTwo.push(e);
        }
      },

      changeRuleThree(e) {
        let show = true;
        this.valueThree = e.parkingDuration+'小时'+e.sellPrice+'元'+'有效期'+e.validityDays+'天';
        this.checkedRulesThree.filter(item => {
          if(item.parkTicketId == e.id || item.id == e.id) {
            show = false
          }
        })
        if(show) {
          this.checkedRulesThree.push(e);
        }
      },

      deleteTicketOne(index) {
        this.checkedRules.splice(index,1)
      },

      deleteTicketTwo(index) {
        this.checkedRulesTwo.splice(index,1)
      },

      deleteTicketThree(index) {
        this.checkedRulesThree.splice(index,1)
      },

      submint(e) {
        let lists = [];
        if(e == 1) {
          this.checkedRules.forEach(item => {
            let list = {};
            list.count = item.count;
            list.parkTicketId = item.id || item.parkTicketId;
            lists.push(list)
          })
        } else if(e == 2) {
          this.checkedRulesTwo.forEach(item => {
            let list = {};
            list.count = item.count;
            list.parkTicketId = item.id || item.parkTicketId;
            lists.push(list)
          })
        } else {
          this.checkedRulesThree.forEach(item => {
            let list = {};
            list.count = item.count;
            list.parkTicketId = item.id || item.parkTicketId;
            lists.push(list)
          })
        }
        this.$axios({
          method: 'post',
          url: '/business/biz-send-park-ticket/edit',
          data: {parkTicketList: lists,type: e}
        }).then((res) => {
          if(res.code == 200) {
            this.$message.success('设置自动发放成功')
          } else {
            this.$message.warning(res.desc)
          }
        })
      }
    }
  }
</script>

<style scoped>
  .automatic_grant {
    height: 100%;
  }

  .content-box {
    height: 100%;
    overflow: auto;
  }

  .ticket-box {
    padding: 10px 20px;
    margin: 20px 0;
    border: 1px solid #bbb;
  }

  .title {
    padding: 0;
    margin: 0;
    font-size: 20px;
    font-weight: bold;
  }

  .el-select {
    margin: 20px 0;
  }

  .ticket-box {
    position: relative;
  }

  .ticket-name {
    padding: 4px;
    display: inline-block;
    padding: 8px 16px;
    margin: 10px;
    font-size: 18px;
    border-radius: 8px;
    border: 1px solid #ccc;
    position: relative;
  }

  .close-icon {
    background-color: #21B5FF;
    color: #fff;
    display: inline-block;
    width: 20px;
    height: 20px;
    text-align: center;
    border-radius: 50%;
    position: absolute;
    right: -6px;
    top: -4px;
  }

  .close-icon:hover {
    cursor: pointer;
  }

  .sub-btn {
    position: absolute;
    top: 50%;
    right: 30px;
    transform: translateY(-50%);
  }






</style>
